<?php
class ManageAction extends BaseAction{
	//初始化;
	public function _initialize(){
		parent::_initialize();
	}	
	//index
	public function index(){
		$this->display();
	}
/**
 +--------------------------------------------------------------------
 * 会员的文章列表页面：调用会员的文章列表;
 +--------------------------------------------------------------------
 * $this->user_id:会员的ID
 +--------------------------------------------------------------------
 */	
	public function article(){
		$article_list= user_articles($this->user_id);
		$this->assign('article_list',$article_list);
		$this->display();
	}
/**
 +--------------------------------------------------------------------
 * 会员的草稿箱：调用会员的文章列表;
 +--------------------------------------------------------------------
 * $this->user_id:会员的ID
 +--------------------------------------------------------------------
 */	
	public function article_draft(){
		$article_list= user_articles($this->user_id);
		$this->assign('article_list',$article_list);
		$this->display();
	}
	
/**
 +--------------------------------------------------------------------
 * 会员的未通过的文章：调用会员的文章列表;
 +--------------------------------------------------------------------
 * $this->user_id:会员的ID
 +--------------------------------------------------------------------
 */	
 	public function article_nopass(){
		$article_list= user_articles($this->user_id);
		$this->assign('article_list',$article_list);
		$this->display();
	}
	
/**
 +--------------------------------------------------------------------
 * 发布文章;
 +--------------------------------------------------------------------
 * $this->user_id:会员的ID
 +--------------------------------------------------------------------
 */	
 
	public function article_post(){
		$this->display();
	}
	
/**
 +--------------------------------------------------------------------
 * 保存修改、添加的文章信息;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	public function article_save(){
		//dump($_POST);die();
		$id = $_POST['id'];
		if($id) $data['id'] = $id;		
		$data['channel'] = $_POST['channel'];
		$data['typeid'] = 228;
		$data['senddate'] = time();
		$data['mid'] = $this->user_id;
		
		$data['Archives']['mid'] = $this->user_id;
		$data['Archives']['writer'] = '寸草园';
		$data['Archives']['title'] = $_POST['title'];
		$data['Archives']['description'] = $_POST['description'];
		$data['Archives']['senddate'] = time();
		$data['Archives']['pubdate'] = time();
		$data['Archives']['keywords'] = '';
		//获取关键词;
		if(trim($_POST['keywords'])){
			$keywords = array_unique(explode(' ',trim($_POST['keywords'])));
			$keywords = implode(',',$keywords);
			$data['Archives']['keywords'] = $keywords;
		}		
		
		
		$data['Addonarticle']['body'] = $_POST['body'];
		//dump($data);
		
		$dao = D('Arctiny');
		if($id){
			$result = $dao->relation(true)->save($data);
		}else{
			$result = $dao->relation(true)->add($data);
			$id = $result;
		}
		
		//保存关键词;
		setKeyword($keywords,$id);
		
		//调用缓存函数，重新生成缓存信息;
		article_info($id,true);
		
		//更新作者文章列表;
		user_articles($this->user_id,true);

		//设置跳转的时间
		$this->assign('waitSecond',100);
		//对$result的返回结果进行判断;
		if($result){
			if($id){
				$this->success('文章修改成功');
			}else{
				$this->success('文章添加成功');
			}
		}else{
			$this->error('有错误发生');
		}
	}
	
/**
 +--------------------------------------------------------------------
 * 修改文章信息;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	
	public function article_edit(){
		$id = $_GET['id']?$_GET['id']:1;
		$article_info = article_info($id);
		$article_info['Archives']['keywords'] = str_replace(',',' ',$article_info['Archives']['keywords']);
		if($article_info['mid'] != $this->user_id){
			$this->error('不是您的文章，您想干嘛啊？');
		}
		$this->assign('article_info',$article_info);
		$this->display();
	}
	
/**
 +--------------------------------------------------------------------
 * 修改文章信息;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
 
	public function edit_my_signature(){
		echo '恭喜你，修改成功! <u>继续修改</u>';
	}
	
/**
 +--------------------------------------------------------------------
 * 修改用户信息;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
 	public function my_info(){
		$dao = D('User_info');
		$my_info_vo = $dao->where('uid = 228')->find();
		$this->assign('my_info',$my_info_vo);
		$this->display();
	}
	
/**
 +--------------------------------------------------------------------
 * 保存修改用户信息;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	public function my_info_save(){
		//dump($_POST);
		$data['penname'] = $_POST['penname'];
		$data['sex'] = $_POST['sex'];
		$data['email'] = $_POST['email'];
		$data['safequestion '] = $_POST['safequestion'];
		$data['safeanswer '] = $_POST['safeanswer'];
		$dao = D('User_info');
		$uid = $_POST['uid'];
		$result = $dao->where('uid='.$uid)->save($data);
		if($result){
			$this->success('修改成功');
		}else{
			$this->error('有错误发生');
		}
	}
	
/**
 +--------------------------------------------------------------------
 * 保存修改用户密码;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	public function my_password(){
		$this->display();
	}
/**
 +--------------------------------------------------------------------
 * 修改用户头像;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	public function my_avatar(){
		$dao = D('User_info');
		$vo = $dao->field('face')->where('uid = 228')->find();
		if($vo){
			$this->assign('vo',$vo);
		}
		$this->display();
	}
/**
 +--------------------------------------------------------------------
 * 保存修改用户头像;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	public function my_avatar_save(){
		if(!empty($_FILES["avatar"]["name"])){
			//判断目录是不是可写
			if(!is_writeable(C('UPLOAD_PATH'))){
				$this->error('目录不可写');
			}
			//判断上传的是不是image
			if(!strstr($_FILES["avatar"]["type"],"image/")){
				$this->error("上传的文件不是一个图片文件");
			}
			//判断文件大小有没有超出;
			elseif($_FILES['avatar']['size']>C('ATTACHMENT_SIZE')){
				$this->error("上传图片大小超过了附件设置所允许的大小");
			}
			//设置用户的上传目录;
			$uploadDir = C('UPLOAD_PATH')."/userup/".'228'."/";
			//判断上传目录是不是存在;
	        if(!is_dir($uploadDir))
			{
				mkdir($uploadDir); //默认的 mode 是 0777
	        }
			//导入image类
			import("ORG.Util.Image");
			
			//设置生成文件名
			$filename = $uploadDir.time().substr($_FILES["avatar"]["name"],strrpos($_FILES["avatar"]["name"],"."));
			//上传文件;
			if(!move_uploaded_file($_FILES["avatar"]["tmp_name"],$filename)){
				$this->error('文件上传保存错误！');
			}
			//生成缩略图
			$avatar =  Image::getImageInfo($filename);
			if($avatar !== false){
				$thumbName = $uploadDir.'myface'.substr($_FILES["avatar"]["name"],strrpos($_FILES["avatar"]["name"],"."));
				$thumbFile = Image::thumb($filename,$thumbName,'',180,180,true);
				if($thumbFile){
					$_POST['face'] = str_replace("./","/",$thumbFile);
				}else{
					$this->error("无法对头像图片进行缩略图处理!");
				}
			}
			$this->assign('waitSecond',100);

			$dao = D('User_info');
			$vo = $dao->create();
			//dump($vo);
			if(!vo){
				$this->error($dao->getError());
			}
			$result = false;
			$result = $dao->save();
			
			if($result){
				$this->success("恭喜你，头像修改成功！");
			}else{
				$this->error('有错误啦');
			}

		}
		
	}
/**
 +--------------------------------------------------------------------
 * 保存修改用户密码;
 +--------------------------------------------------------------------
 * $id:文章ID
 +--------------------------------------------------------------------
 */	
	public function my_password_save(){
		$where['uid'] = $_POST['uid'];
		$where['password'] = md5($_POST['password_old']);
		$data['password'] = md5($_POST['password']);
		
		$dao = D('User');
		$result = $dao->where($where)->find();
		if($result){
			$result2 = $dao->where($where)->save($data);
			if($result2){
				$this->success('修改密码成功');
			}else{
				$this->error('修改失败');
			}
		}else{
			$this->error('原密码错误，请重新输入');
		}
	}
	
/**
 +--------------------------------------------------------------------
 * 检查关键词是不是合法 ajax
 +--------------------------------------------------------------------
 */
	public function checkKeywords(){
		$tag = checkTag($_POST['keywords']);
		echo $tag;
	}
	
/**
 +--------------------------------------------------------------------
 * 用户后台反馈bug;
 +--------------------------------------------------------------------
 */
 	public function bug(){
		$dao = D('Doing');
		$vo = $dao->findall();
		$this->assign('vo',$vo);
		$this->display();
	}
	
/**
 +--------------------------------------------------------------------
 * 用户的收藏夹，图书馆;
 +--------------------------------------------------------------------
 */
 	public function library(){
		echo 'hi';
	}
/**
 +--------------------------------------------------------------------
 * 添加收藏;
 +--------------------------------------------------------------------
 */
 	public function library_add(){
		if($_GET['aid']){
			$arcDao = D('Archives');
			$arcVo = $arcDao->where('id='.$_GET['aid'])->find();
			$this->assign('arcVo',$arcVo);
			//dump($arcVo);
		}
		$this->display();
	}
/**
 +--------------------------------------------------------------------
 * 添加收藏;
 +--------------------------------------------------------------------
 */
 	public function library_save(){
		$libDao = D('Library');
		$data = $_POST;
		$data['dateline'] = time();
		$data['uid'] = 228;
		$libVo = $libDao->add($data);
		echo '添加成功啦';
	}
/**
 +--------------------------------------------------------------------
 * 空操作
 +--------------------------------------------------------------------
 */
	public function _empty(){
		$this->error('抱歉，您查找的页面不存在。');
	}
}
?>